package demo2;
//抽象类和接口的区别(常见面试题)
//

import java.util.Objects;

/*public interface Cloneable {
    //空接口，标记接口：作用就是表示当前对象就是可以被克隆的
}*/
class Money implements Cloneable {
    public double money = 12.25;

    @Override
    protected Object clone() throws CloneNotSupportedException {

        return super.clone();
    }
}
class Student implements Cloneable {

    public String name;
    public Money m = new Money();

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // 只是克隆了student对象
        Student student = (Student)super.clone();
        // 克隆了student对象里面的Money对象
        student.m = (Money) this.m.clone();
        return student;
        //return super.clone();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name);
    }


    /*public boolean equals(Object obj) {
      *//* Student student = (Student) obj;
       if (this.name.equals(student.name)) {
           return true;
       }
       return false;*//*
        if (obj == null) {
            return false ;
        }
        //指向的是同一个对象
        if(this == obj) {
            return true ;
        }
        // 不是Student类对象
        if (!(obj instanceof Student)) {
            return false ;
        }

    }*/


    @Override
    public int hashCode() {
        return Objects.hash(name, m);
    }
}

class Person {}
class Animal{}
class Dog{
    public void func1(){}
    public void func2(){}
    public void func3(){}
};

public class Test {

    public static void main(String[] args) {
        Student student1 = new Student();
        student1.name = "zhangsan";

        Student student2 = new Student();
        student2.name = "zhangsan";

        boolean flg = student1.equals(student2);
        System.out.println("flg:" + flg);
    }

    public static void func(Object obj) {

    }

    public static void main3(String[] arg) {
        //匿名对象  缺点：每次使用都需要new  使用场景：需要只使用一次的时候
        func(new Money());
        func(new Person());
        func(new Animal());
        func(new Dog());
        //非匿名对象
        Dog dog = new Dog();
        dog.func1();
        dog.func2();
        dog.func3();
        System.out.println("============");
        //如果是匿名对象
        new Dog().func1();
        new Dog().func2();
        new Dog().func3();
    }

    public static void main2(String[] args) throws CloneNotSupportedException{
        Student student1 = new Student();
        Student student2 = (Student) student1.clone();
        System.out.println("修改前：");
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
        System.out.println("=================");
        student2.m.money = 99;
        System.out.println("修改后：");
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
    }

    public static void main1(String[] args) throws CloneNotSupportedException{
        Student student1 = new Student();
        student1.name = "asdf";

        Student student2 = (Student) student1.clone();

        System.out.println(student1);
        System.out.println(student2);
        Object o = new Student();
    }
}
